package service.impl;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import dao.ITradeDao;
import dao.IUserDao;
import model.Trade;
import model.User;
import org.apache.ibatis.executor.ReuseExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.IUserService;
import util.DateUtil;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserService implements IUserService {
    @Autowired
    private IUserDao iUserDao;
    @Autowired
    private ITradeDao iTradeDao;
    @Override
    public User login(User user) {
        if(iUserDao.login(user).size()!=0){
            return  iUserDao.login(user).get(0);
        }else{
            return null;
        }
    }

    @Override
    public int register(User user) {
        return iUserDao.register(user);
    }

    @Override
    public String recharge(User user, String recharge) {
        BigDecimal money = new BigDecimal(recharge);
        Trade trade = new Trade();
        trade.setTradeMoney(money);
        trade.setTradeType(1);
        trade.setuId(user.getId());
        trade.setOrderNo(DateUtil.dateFormat()+user.getuPhone().substring(7,11));
        trade.setCreateTime(DateUtil.currentDate());
        user.setuMoney(money.add(user.getuMoney()));
       int count = iUserDao.updateMoney(user);
        System.out.println("------------------------");
        System.out.println(trade);
       int count1 = iTradeDao.insertTrade(trade);
       if (count >=1 && count1 >= 1){
           return  "1";
       }else{
           return  "0";
       }
    }

    @Override
    public String changePass(User user) {
        int count = iUserDao.changePass(user);
        if(count <1){
            return "0";
        }else{
            return  "1";
        }
    }
}
